package utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionFactoryUtils {
	static SqlSessionFactory sqlSessionFactory=null;
	private SqlSessionFactoryUtils(){}
	public static SqlSessionFactory getSqlSessionFactory(){
		if(sqlSessionFactory!=null){
			return sqlSessionFactory;
		}
		String configName="mybatisConfig.xml";
		InputStream inputStream;
		try {
			inputStream =Resources.getResourceAsStream(configName);
			sqlSessionFactory=	new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sqlSessionFactory;
	}
	public static SqlSession openSqlSession(){
		if(sqlSessionFactory==null){
			getSqlSessionFactory();
		}
		return sqlSessionFactory.openSession();
	}
	
	
	
}
